package zhoug.demo.zui.ui;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import zhoug.common.adapters.recycler.layoutmanager.FlowLayoutManager;
import zhoug.common.beans.IntegerNameValue;
import zhoug.common.interfaces.NameTransformer;
import zhoug.common.json.JsonUtils;
import zhoug.common.utils.CollectionUtils;
import zhoug.common.utils.ToastUtils;
import zhoug.demo.zui.Area;
import zhoug.demo.zui.DataGenerator;
import zhoug.demo.zui.R;
import zhoug.logging.Logger;
import zhoug.zui.input.ZUIInputCheckView;
import zhoug.zui.input.ZUIInputTextView;
import zhoug.zui.list.CheckListAdapter;
import zhoug.zui.list.OnSelectChangeListener;

/**
 * @Author: zhoug
 * @Date: 2024-07-08
 * @Description:
 */
public class ZUIInputCheckViewActivity extends ZUIInputViewBaseActivity {
    private static final String TAG = ">>>ZUIInputAllActivity";
    private Switch switchEnable;
    private Switch switchError;
    private EditText editText1;
    private ZUIInputTextView inputTextView1;
    private ZUIInputCheckView inputCheckViewGender;
    private ZUIInputCheckView inputCheckViewCheckBox;
    private ZUIInputCheckView inputCheckViewArea;
    private ImageView imageCheckBox;
    private Switch switchMulti;
    private ZUIInputCheckView inputCheckViewLayoutType;
    private RadioButton rbtnFlow;
    private RadioButton rbtnH;
    private RadioButton rbtnV;
    private RadioButton rbtnGrid;
    private RadioButton rbtnStaggered;
    private RadioGroup radioGroup;
    private LinearLayout contentView;


    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_input_check_view);
        findViews();
//        printPadding();

        initialize();

        imageCheckBox.setOnClickListener(v -> {
            v.setSelected(!v.isSelected());
        });
    }

    private void initialize() {
        switchMulti.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                inputCheckViewGender.setMultiSelect(isChecked);
                inputCheckViewArea.setMultiSelect(isChecked);
            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbtn_h:
                        inputCheckViewGender.setLayoutManager(new LinearLayoutManager(ZUIInputCheckViewActivity.this, RecyclerView.HORIZONTAL, false));
                        inputCheckViewArea.setLayoutManager(new LinearLayoutManager(ZUIInputCheckViewActivity.this, RecyclerView.HORIZONTAL, false));
                        break;
                    case R.id.rbtn_v:
                        inputCheckViewGender.setLayoutManager(new LinearLayoutManager(ZUIInputCheckViewActivity.this));
                        inputCheckViewArea.setLayoutManager(new LinearLayoutManager(ZUIInputCheckViewActivity.this));
                        break;
                    case R.id.rbtn_grid:
                        inputCheckViewGender.setLayoutManager(new GridLayoutManager(ZUIInputCheckViewActivity.this, 3));
                        inputCheckViewArea.setLayoutManager(new GridLayoutManager(ZUIInputCheckViewActivity.this, 3));
                        break;
                    case R.id.rbtn_Staggered:
                        inputCheckViewGender.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
                        inputCheckViewArea.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
                        break;
                    default:
                        inputCheckViewGender.setLayoutManager(new FlowLayoutManager(true));
                        inputCheckViewArea.setLayoutManager(new FlowLayoutManager(true));
                        break;
                }
            }
        });

        inputCheckViewGender.setOnSelectChangeListener(new OnSelectChangeListener<NameTransformer>() {
            @Override
            public void onSelectChange(@NonNull List<NameTransformer> select) {
                ToastUtils.toastLong(CollectionUtils.listToNames(select));
            }

            @Override
            public void onClearSelect() {
                ToastUtils.toastLong("没有选择");
            }
        });
        List<Area> areas = DataGenerator.getAreas1();
        Area area = areas.get(2);
        Area defSelect=new Area(area.getName(), area.getCode());
        inputCheckViewArea.setSelect(defSelect);
        inputCheckViewArea.setData(areas);
        inputCheckViewArea.setMultiSelect(false);
        inputCheckViewArea.setOnSelectChangeListener(new OnSelectChangeListener<NameTransformer>() {
            @Override
            public void onSelectChange(@NonNull List<NameTransformer> select) {
                ToastUtils.toastLong(CollectionUtils.listToNames(select));
                ArrayList<Area> areas = CollectionUtils.parseList(select, Area.class);
                Logger.d(TAG, "onSelectChange:"+ JsonUtils.toJson(areas));
            }

            @Override
            public void onClearSelect() {
                ToastUtils.toastLong("没有选择");

            }
        });
        List<IntegerNameValue> data1=new ArrayList<>();
        data1.add(new IntegerNameValue("10-18", 1));
        data1.add(new IntegerNameValue("18-30", 2));
        data1.add(new IntegerNameValue("30-60", 3));
        inputCheckViewCheckBox.setMultiSelect(true);
        inputCheckViewCheckBox.setData(data1);
        inputCheckViewCheckBox.setOnSelectChangeListener(new OnSelectChangeListener<NameTransformer>() {
            @Override
            public void onSelectChange(@NonNull List<NameTransformer> select) {
                ToastUtils.toastLong(CollectionUtils.listToNames(select));
                Logger.d(TAG, "onSelectChange:"+ JsonUtils.toJson(areas));
            }

            @Override
            public void onClearSelect() {
                ToastUtils.toastLong("没有选择");

            }
        });

        List<IntegerNameValue> data=new ArrayList<>();
        IntegerNameValue text = new IntegerNameValue("TEXT", CheckListAdapter.TEXT);
        data.add(new IntegerNameValue("TEXT", CheckListAdapter.TEXT));
        data.add(new IntegerNameValue("RADIO_BUTTON", CheckListAdapter.RADIO_BUTTON));
        data.add(new IntegerNameValue("CHECK_BOX", CheckListAdapter.CHECK_BOX));
        inputCheckViewLayoutType.setData(data);
        inputCheckViewLayoutType.setMultiSelect(false);
        inputCheckViewLayoutType.setSelect(text);
        inputCheckViewLayoutType.setOnSelectChangeListener(new OnSelectChangeListener<NameTransformer>() {
            @Override
            public void onSelectChange(@NonNull List<NameTransformer> select) {
                IntegerNameValue nameTransformer = (IntegerNameValue) select.get(0);
                inputCheckViewGender.setLayoutType(nameTransformer.getData());
                inputCheckViewGender.notifyDataChange();
                inputCheckViewArea.setLayoutType(nameTransformer.getData());
                inputCheckViewArea.notifyDataChange();
                inputCheckViewGender.requestLayout();
                inputCheckViewArea.requestLayout();
            }

            @Override
            public void onClearSelect() {

            }
        });
    }


    @Override
    protected ViewGroup getContentView() {
        return contentView;
    }

    @Override
    protected Switch getEnableSwitchView() {
        return switchEnable;
    }

    @Override
    protected Switch getErrorSwitchView() {
        return switchError;
    }


    private void findViews() {
        switchEnable = findViewById(R.id.switch_enable);
        switchError = findViewById(R.id.switch_error);
        editText1 = findViewById(R.id.editText1);
        inputTextView1 = findViewById(R.id.inputTextView1);
        inputCheckViewGender = findViewById(R.id.inputCheckViewGender);
        inputCheckViewCheckBox = findViewById(R.id.inputCheckViewCheckBox);
        inputCheckViewArea = findViewById(R.id.inputCheckViewArea);
        imageCheckBox = findViewById(R.id.imageCheckBox);
        switchMulti = findViewById(R.id.switch_multi);
        inputCheckViewLayoutType = findViewById(R.id.inputCheckViewLayoutType);
        rbtnFlow = findViewById(R.id.rbtn_flow);
        rbtnH = findViewById(R.id.rbtn_h);
        rbtnV = findViewById(R.id.rbtn_v);
        rbtnGrid = findViewById(R.id.rbtn_grid);
        rbtnStaggered = findViewById(R.id.rbtn_Staggered);
        radioGroup = findViewById(R.id.radioGroup);
        contentView = findViewById(R.id.contentView);
    }
}
